-- @owner:yangjian
-- @date:2024-4-7
-- @testpoint:1.去除大对象使用限制，测试lo_tell函数使用和参数的表现，合理报错

--清理环境
CREATE OR REPLACE FUNCTION f_opengauss_large_object_function_case0011_1()  
RETURNS VOID AS 
$$
BEGIN  
    BEGIN
    PERFORM lo_unlink(100);
    EXCEPTION
        WHEN others THEN
            NULL;
    end;
END;  
$$
LANGUAGE plpgsql;
/

select f_opengauss_large_object_function_case0011_1();

--环境准备
drop table if exists t_opengauss_large_object_function_case0011_1 cascade;
CREATE TABLE t_opengauss_large_object_function_case0011_1 (loid oid,fd int);
INSERT INTO t_opengauss_large_object_function_case0011_1 (loid) SELECT lo_create(100);

--step1:测试fd参数为存在值;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek64(0,5,0);
select lo_tell(0);
end;
/

--step2:测试fd参数为不存在值;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek64(0,5,0);
select lo_tell(1);
end;
/
end;
--step3:测试fd参数为非int类型;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek64(0,5,0);
select lo_tell('a');
end;
/
end;
--step4:测试fd参数为负数;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek64(0,5,0);
select lo_tell(-1);
end;
/
end;
--step5:测试fd参数为空值;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek64(0,5,0);
select lo_tell(null);
end;
/

--step6:测试fd参数超过int4;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek64(0,5,0);
select lo_tell(2147483648);
end;
/
end;
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek64(0,5,0);
select lo_tell(-2147483649);
end;
/
end;
--step7:测试超过2g;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lo_lseek64(0,4294967296,0);
select lo_tell(0);
end;
/
end;
--清理环境
select lo_unlink(loid) from t_opengauss_large_object_function_case0011_1;
select f_opengauss_large_object_function_case0011_1();
drop table if exists t_opengauss_large_object_function_case0011_1 cascade;
drop function f_opengauss_large_object_function_case0011_1();
